package com.aier.admin_new.action;

import java.io.File;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.tools.ant.types.resources.selectors.Date;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.common.AndroidType;
import com.aier.admin_new.common.CompresszZipFile;
import com.aier.admin_new.common.MessageBaiduYunSendTransmitter;
import com.aier.admin_new.service.IMessageNService;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.common.common.SystemConfigStatic;
import com.aier.user.entity.Letter;
import com.aier.user.entity.LetterDetail;
import com.aier.user.entity.User;
import com.aier.user.entity.mapper.LetterRowMapper;
import com.aier.user.service.IUserService;
import com.google.gson.JsonObject;


@Scope("prototype")
@Component("messageNAction")
public class MessageNAction extends AdminNewBaseAction{
	
    @Resource(name="messageNService")
    private IMessageNService messageNService;
    @Resource(name="userService")
    private IUserService userService;
    
    // 错误码
    private int s;
	// 收信人的用户id
    private Long ruid;
    //  信人的用户id
    private Long suid;
    // 私信的内容
    private String content;
    // 上传的图片名称
    private String photoName;

    // 页数
    private int page;
    // 每页的记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总记录数
    private int count;
    
    // 所有聊天列表
    private List<Letter> letterList;
    // 私信id
    private Long lid;
    
    // 某个聊天记录详情
    private List<LetterDetail> ldList;
    
    private Long uid;
    private File file;
    
    private String cdate;//发送时间
    private int  kind  ;//种类 1 图片  0 文字消息
    private int  belongId;//消息属于谁的
    
	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	
   /**
     * 发送聊天
     * @return
     */
    public String sendLetter() {
    	//上传图片
    	String img = "";
    	if(file !=null){
    		//http://www.1.com:8080/aier360/img/v1/parenting/ 获取文件夹
    		img  = CompresszZipFile.uploadDailyfoodImgApp(file, photoName, SystemConfigStatic.parenting_upload_url);
    	}
    		s=1;
    		int lid = messageNService.insertMessage(suid,ruid, (short)1, content, (short)0, img);   		
    		boolean isOnlie=userService.serachUserOnline(ruid);
        //用户不在线以后在说
    		if(isOnlie==false){
    			s=0;
    		}
    		
    		com.aier.user.entity.User suser=userService.searchBabyByUserid(suid);
    		com.aier.user.entity.User user=userService.getAppUid(ruid);
    		MessageBaiduYunSendTransmitter mbysst = new MessageBaiduYunSendTransmitter();
    		
    		String description="";
    		
    		JsonObject json = new JsonObject();
	    	if(kind==0){
	    		json.addProperty("lid", lid);
	    		json.addProperty("suid", suid);
	    		json.addProperty("ruid", ruid);
	    		json.addProperty("content", content);
	    		json.addProperty("img", img);
	    		json.addProperty("kind", 0);//文本
	    		json.addProperty("belongId", belongId);//
	    		json.addProperty("cdate", cdate);
	    		json.addProperty("headimg", suser.getHeadimg());
	    		description=content+"#"+json.toString();
	    	}else if(kind==1){
	    		json.addProperty("lid", lid);
	    		json.addProperty("suid", suid);
	    		json.addProperty("ruid", ruid);
	    		json.addProperty("img", img);
	    		json.addProperty("kind", 1);//图片
	    		json.addProperty("belongId", belongId);//
	    		json.addProperty("cdate", cdate);
	    		json.addProperty("headimg", suser.getHeadimg());
	    		description="[图片]#"+json.toString();
	    	}
	    	
		mbysst = new MessageBaiduYunSendTransmitter(user, user.getNickname(),  description, AndroidType.MESSAGENEW, suid, 2);
		mbysst.sendServer();
		
        return SUCCESS;
    }
    
    /**
     * 获取所有聊天的列表
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchFollowLetter() {
        List<Letter> map = messageNService.searchFollowMessage(suid);//获取自己的
        
        if (null != map) {
            letterList=map;
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 跟某个人的聊天记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchLetterDetail() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        
        Map<String, Object> map = messageNService.searchMessageDetail(page, page_size, lid);
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            ldList = (List<LetterDetail>) map.get(ICommonDAO.OBJ_LIST);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 系统信息
     * @return
     */
    public String searchSystembaseMessage(){
        List map = messageNService.searchSystemMessage(suid);//获取自己的
        if (null != map) {
            letterList=map;
        }
        s = 1;
    	return SUCCESS;
    }
    
    /**
     * 清空私信
     * @return
     */
    public String deleteLetter() {
        s = messageNService.deleteMessage(suid);
        return SUCCESS;
    }

    public void setRuid(Long ruid) {
        this.ruid = ruid;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<Letter> getLetterList() {
        return letterList;
    }

    public void setLid(Long lid) {
        this.lid = lid;
    }

    public List<LetterDetail> getLdList() {
        return ldList;
    }

    public int getS() {
        return s;
    }

    public void setPhotoName(String photoName) {
		this.photoName = photoName;
	}

	public int getCount() {
        return count;
    }

    public Long getUid() {
        return uid;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public Long getLid() {
        return lid;
    }

    public Long getRuid() {
        return ruid;
    }

    public Long getSuid() {
        return suid;
    }

    public void setSuid(Long suid) {
        this.suid = suid;
    }


    public void setPage(int page) {
        this.page = page;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage() {
        return page;
    }
    
    public void setUid(Long uid) {
 		this.uid = uid;
 	}

	public void setFile(File file) {
		this.file = file;
	}

	public void setCdate(String cdate) {
		this.cdate = cdate;
	}

	public void setKind(int kind) {
		this.kind = kind;
	}

	public void setBelongId(int belongId) {
		this.belongId = belongId;
	}
	
}